<?php 
    App::import('Model', 'Movimientostrabajo');
    $modeloMovTrabajo = new Movimientostrabajo();
?>
<style>
    /*    ventana sacar*/
    #element_to_pop_up{ 
        background-color:#fff;
        box-shadow: 0px 2px 3px rgba(0, 0, 0, 0.298);
        border-radius:5px;
        color:#000;
        display:none; 
        padding:5px;
        min-width:300px;
        min-height: 180px;        
    }

    /*    ventana vender*/
    #element_to_pop_up_venta{ 
        background-color:#fff;
        box-shadow: 0px 2px 3px rgba(0, 0, 0, 0.298);
        border-radius:5px;
        color:#000;
        display:none; 
        padding:5px;
        min-width:300px;
        min-height: 180px;        
    }
    .b-close{
        cursor:pointer;
        position:absolute;
        right:10px;
        top:5px;            
    }   
</style>
<?php foreach ($detalleValdes as $dv): ?>
    <style>
        #element_to_pop_up_saca_<?php echo $dv['Movimientosvalde']['id']; ?>{ 
            background-color:#fff;
            box-shadow: 0px 2px 3px rgba(0, 0, 0, 0.298);
            border-radius:5px;
            color:#000;
            display:none; 
            padding:5px;
            min-width:300px;
            min-height: 180px;        
        }  
    </style>  
<?php endforeach; ?>

<script type="text/javascript">
    jQuery(document).ready(function() {
        var sheepItForm = jQuery('#sheepItForm').sheepIt({
            separator: '',
            allowRemoveLast: true,
            allowRemoveCurrent: true,
            allowRemoveAll: true,
            allowAdd: true,
            allowAddN: true,
            maxFormsCount: 20,
            minFormsCount: 1,
            iniFormsCount: 1
        });

        var sheepItForm = jQuery('#sheepItFormV').sheepIt({
            separator: '',
            allowRemoveLast: true,
            allowRemoveCurrent: true,
            allowRemoveAll: true,
            allowAdd: true,
            allowAddN: true,
            maxFormsCount: 20,
            minFormsCount: 1,
            iniFormsCount: 1
        });
    });
</script>   

<!-- formulario salida -->
<div id="element_to_pop_up" style="display: none;">      
    <div class="grid12">
        <div class="widget">
            <h5>SACAR PRODUCTO: <?php echo $datosValde['Valde']['nombre']; ?></h5>
            <h6>PESO: <?php echo $datosValde['Valde']['peso']; ?> Kilos</h6>
            <a class="b-close">X</a> 
            <?php echo $this->Form->create('Movimientosvaldes', array('action' => 'registrarsalidas')); ?>
            <div id="sheepItForm">

                <!-- Form template-->
                <div id="sheepItForm_template">
                    <table>
                        <tr>
                            <td><b>CODIGO DEL PRODUCTO # <span id="sheepItForm_label"></span>: &nbsp;&nbsp;</b></td>
                            <td>
                                <?php echo $this->Form->text(null, array('name' => "data[Movimientosvaldes][#index#][codigo]", 'required', 'style' => 'width: 120px;')); ?>
                                <?php echo $this->Form->hidden(null, array('name' => "data[Movimientosvaldes][#index#][valde_id]", "value" => $datosValde['Valde']['id'])); ?>
                                <?php echo $this->Form->select('Movimientosvaldes.#index#.sucursale_id', $dcs); ?>
                            </td>                            
                            <td>
                                <a id="sheepItForm_remove_current">
                                    <b>&nbsp;&nbsp;X</b>
                                </a>
                            </td>
                        </tr>
                        <tr>
                            <td>
                                &nbsp;
                            </td>
                        </tr>
                    </table>                                                                                            
                    <div class="clear"></div>                    
                </div>
                <!-- /Form template-->
                <div class="formRow">
                    <!-- No forms template -->
                    <div id="sheepItForm_noforms_template">Sin empleados</div>
                    <!-- /No forms template-->
                    <!-- Controls -->
                    <div id="sheepItForm_controls">
                        <div id="sheepItForm_add" class="btn form add"><a href="#" class="buttonS bGreen "><b>+</b> A&ntilde;adir mas Productos</a></div>
                        <div id="sheepItForm_remove_last" class="btn form remove"><a href="#" class="buttonS bRed "><b>-</b> Remover Producto</a></div>
                        <div id="sheepItForm_remove_all" class="btn form removeAll"><a href="#" class="buttonS bGold "><b>-</b> Remover Todos</a></div>

                        <div id="sheepItForm_add_n">
                            <input id="sheepItForm_add_n_input" type="text" size="4" style="width: 35px;" />
                            <div id="sheepItForm_add_n_button"><a><a href="#" class="buttonS bBlue"><b>+</b> A&ntilde;adir mas Productos</a></a></div></div>
                    </div>
                </div>
                <!-- /Controls -->
            </div>
            <input type="submit" value="Registrar Salidas" class="buttonM bBlack formSubmit"><div class="clear"></div>            
            </form>

        </div>
    </div>
</div>
<!-- fin formulario salida -->

<!-- formulario venta -->
<div id="element_to_pop_up_venta" style="display: none;">      
    <div class="grid12">
        <div class="widget">
            <h5>VENDER PRODUCTO: <?php echo $datosValde['Valde']['nombre']; ?></h5>
            <h6>PESO: <?php echo $datosValde['Valde']['peso']; ?> Kilos</h6>
            <a class="b-close">X</a> 
            <?php echo $this->Form->create('Movimientosvaldes', array('action' => 'registrarventas')); ?>
            <div id="sheepItFormV">

                <!-- Form template-->
                <div id="sheepItFormV_template">
                    <table>
                        <tr>
                            <td><b>PRODUCTO # <span id="sheepItFormV_label"></span>: &nbsp;&nbsp;</b></td>
                            <td>                                
                                Cliente
                                <?php echo $this->Form->text(null, array('name' => "data[Movimientosvaldes][#index#][cliente]", 'style' => "width: 120px;", 'required')); ?>
                                &nbsp;&nbsp;&nbsp;
                                Codigo
                                <?php echo $this->Form->text(null, array('name' => "data[Movimientosvaldes][#index#][codigo]", 'style' => "width: 125px;", 'required')); ?>
                                <?php echo $this->Form->hidden(null, array('name' => "data[Movimientosvaldes][#index#][valde_id]", "value" => $datosValde['Valde']['id'])); ?>
                                &nbsp;&nbsp;&nbsp;
                                Precio
                                <?php echo $this->Form->text(null, array('name' => "data[Movimientosvaldes][#index#][precio]", 'style' => "width: 55px;", 'required')); ?>
                            </td>                            
                            <td>
                                <a id="sheepItFormV_remove_current">
                                    <b>&nbsp;&nbsp;X</b>
                                </a>
                            </td>
                        </tr>
                        <tr>
                            <td>
                                &nbsp;
                            </td>
                        </tr>
                    </table>
                    <div class="clear"></div>                    
                </div>
                <!-- /Form template-->
                <div class="formRow">
                    <!-- No forms template -->
                    <div id="sheepItFormV_noforms_template">Sin empleados</div>
                    <!-- /No forms template-->
                    <!-- Controls -->
                    <div id="sheepItFormV_controls">
                        <div id="sheepItFormV_add" class="btn form add"><a href="#" class="buttonS bGreen "><b>+</b> A&ntilde;adir mas Productos</a></div>
                        <div id="sheepItFormV_remove_last" class="btn form remove"><a href="#" class="buttonS bRed "><b>-</b> Remover Producto</a></div>
                        <div id="sheepItFormV_remove_all" class="btn form removeAll"><a href="#" class="buttonS bGold "><b>-</b> Remover Todos</a></div>

                        <div id="sheepItFormV_add_n">
                            <input id="sheepItFormV_add_n_input" type="text" size="4" style="width: 35px;" />
                            <div id="sheepItFormV_add_n_button"><a><a href="#" class="buttonS bBlue"><b>+</b> A&ntilde;adir mas Productos</a></a></div></div>
                    </div>
                </div>
                <!-- /Controls -->
            </div>
            <input type="submit" value="Registrar Salidas" class="buttonM bBlack formSubmit"><div class="clear"></div>            
            </form>

        </div>
    </div>
</div>
<!-- fin formulario salida -->

<div class="grid12">
    <div class="widget">
        <div class="whead"><h6>DETALLE DEL PRODUCTO</h6><div class="clear"></div></div>
        <div class="body">
            <table cellpadding="0" cellspacing="0" width="100%" class="tLight noBorderT">
                <tr>
                    <td><h4>Nombre</h4></td>
                    <td><h3><?php echo $datosValde['Valde']['nombre']; ?></h3></td>
                </tr>
                <tr>
                    <td><h4>Peso</h4></td>
                    <td>
                        <h3>
                            <?php 
                            echo $datosValde['Valde']['peso'];                                 
                            ?> Kilos
                        </h3>
                    </td>
                </tr>
                <tr>
                    <td>
                        <h4>Cantidad Almacen <?php echo $this->Session->read('Auth.User.Sucursale.nombre'); ?></h4>
                    </td>
                    <td><h3>
                            <?php
                            $sucursalUsuario = $this->Session->read('Auth.User.sucursale_id');

                            if ($sucursalUsuario == 6)
                            {
                                $validaCantidad = $cantidadValdes['Movimientosvalde']['cantidad'];
                                if ($validaCantidad == 0)
                                {
                                    echo 'No tiene';
                                } else
                                {
                                    echo $validaCantidad;
                                }
                            } else
                            {
                                echo $cantidadValdesSucursal;
                            }
                            ?> unidades
                        </h3></td>
                </tr>
            </table> 
            <br />
            <?php if ($validaCantidad > 0 || $cantidadValdesSucursal > 0): ?>
                <?php
                $rol = $this->Session->read('Auth.User.role');
                if ($rol == 'admin'):
                    ?>
                    <div align="center">          
                        <a class="buttonS bRed" id="btSacaAlmacen">SACAR PRODUCTO</a>&nbsp;&nbsp;
                        <a class="buttonS bGreen" id="btVenta">VENDER PRODUCTO</a>&nbsp;&nbsp;                
                    </div>
                <?php else: ?>
                <?php endif; ?>
            <?php else: ?>
            <?php endif; ?>
        </div>
    </div>
</div>
<div class="grid12">
    <div class="widget">
        <div class="whead"><h6>LISTADO DE PRODUCTOS</h6><div class="clear"></div></div>
        <div class="shownpars">
            <?php if (!empty($detalleValdes)): ?>
                <table cellpadding="0" cellspacing="0" border="0" class="dTable">
                    <thead>
                        <tr>
                            <th>CODIGO</th> 
                            <th>PESO</th> 
                            <th>SUCURSAL</th>
                            <th>ACCIONES</th>                  
                        </tr>
                    </thead>
                    <tbody>
                        <tr>
                            <?php
                            $i = 1;
                            foreach ($detalleValdes as $dv):
                                ?>                    
                                <td><?php echo $dv['Movimientosvalde']['codigo']; ?></td>                            
                                <td>
                                    <?php
                                    $pesoValde = $modeloMovTrabajo->find('first', array(
                                        'recursive'=>-1,
                                        'conditions'=>array('Movimientostrabajo.movimientosvalde_id'=>$dv['Movimientosvalde']['id']),
                                        'order'=>'Movimientostrabajo.id DESC'
                                    ));
                                    if(!empty($pesoValde)){
                                        echo $pesoValde['Movimientostrabajo']['sobra'];                                                
                                    }else{
                                        echo $dv['Movimientosvalde']['peso'];
                                    }
                                    ?> 
                                Kilos</td>                            
                                <td><?php echo $dv['Sucursale']['nombre']; ?></td>
                                <td>
                                    <?php $rol = $this->Session->read('Auth.User.role'); ?>
                                    <?php if ($rol == 'admin' && $sucursalUsuario==$dv['Sucursale']['id']): ?>
                                        <a onclick="ajaxSalida<?php echo $dv['Movimientosvalde']['id']; ?>();"><?php echo $this->Html->image("iconos/salida.png", array('title' => 'Sacar Producto')) ?></a>
                                        <?php echo $this->Html->link($this->Html->image("iconos/edit.png", array("alt" => 'Editar', 'title' => 'Editar')), array('action' => 'editar', $dv['Movimientosvalde']['id']), array('escape' => false)); ?>
                                        <?php echo $this->Html->link($this->Html->image("iconos/elim.png", array("alt" => 'Eliminar', 'title' => 'Eliminar')), array('action' => 'eliminar', $dv['Movimientosvalde']['id']), array('escape' => false), ("Desea eliminar realmente??")); ?>                                                                                            
                                    <?php else: ?>
                                    <?php endif; ?>
                                </td>
                            </tr>

                        <?php endforeach; ?>                
                    </tbody>
                </table>
            <?php else: ?>
                <h1>No tiene Productos</h1>
            <?php endif; ?>
            <?php foreach ($detalleValdes as $dv): ?>            
                <div id="element_to_pop_up_saca_<?php echo $dv['Movimientosvalde']['id']; ?>">
                    <a class="b-close">X</a>                    
                    <div class="widget grid12">
                        <div class="whead"><h6>SALIDA DE ALMACEN </h6><div class="clear"></div></div>
                        &nbsp;
                        <h4>&nbsp;&nbsp; PRODUCTO: <?php echo $datosValde['Valde']['nombre']; ?></h4>
                        <h5>&nbsp;&nbsp; Peso: <?php echo $dv['Movimientosvalde']['peso']; ?> Kilos</h5>
                        <?php echo $this->Form->create('Movimientosvaldes', array('action' => 'sacarliquido', 'id' => "validate")); ?>
                        <div class="formRow">                                                                                                                
                            <div class="grid3"><label>Enviar A:<span class="req">*</span></label></div>
                            <div class="grid3">
                                <?php echo $this->Form->select('sucursale_id', $dcs, array('required')); ?>
                                <?php echo $this->Form->hidden('valde_id', array('value' => $dv['Movimientosvalde']['id'])); ?>
                            </div>
                        </div>    
                        <div class="clear"></div>
                        <div class="formRow">
                            <input type="submit" value="Registrar Salida" class="buttonS bGreen" /><div class="clear"></div>            
                        </div>
                        </form>
                    </div>
                </div>
                <script>
    function ajaxSalida<?php echo $dv['Movimientosvalde']['id']; ?>()
    {
        // Binding a click event
        // From jQuery v.1.7.0 use .on() instead of .bind()
        //$('#btAjaxGeneraSalida_<?php //echo $ct['Cortestubo']['id'];          ?>').bind('click', function(e) {
        console.log('click');
        // Triggering bPopup when click event is fired
        $('#element_to_pop_up_saca_<?php echo $dv['Movimientosvalde']['id']; ?>').bPopup({
            fadeSpeed: 'slow', //can be a string ('slow'/'fast') or int
            followSpeed: 1500, //can be a string ('slow'/'fast') or int                                             
            //$('#element_to_pop_up_<?php //echo $t['Tubo']['id'];                  ?>').bPopup()
        });
        //});    
    }
                </script>
            <?php endforeach; ?>
        </div> 
    </div> 
</div>

<script>
    $(document).ready(function() {
        // Binding a click event
        // From jQuery v.1.7.0 use .on() instead of .bind()
        $('#btSacaAlmacen').bind('click', function(e) {

            // Triggering bPopup when click event is fired
            $('#element_to_pop_up').bPopup({
                fadeSpeed: 'slow', //can be a string ('slow'/'fast') or int
                followSpeed: 1500, //can be a string ('slow'/'fast') or int 
                width: 20,
                position: ['auto', 40] //x, y
                        //$('#element_to_pop_up_<?php //echo $t['Tubo']['id'];                                      ?>').bPopup()
            });
        });

        $('#btVenta').bind('click', function(e) {

            // Triggering bPopup when click event is fired
            $('#element_to_pop_up_venta').bPopup({
                fadeSpeed: 'slow', //can be a string ('slow'/'fast') or int
                followSpeed: 1500, //can be a string ('slow'/'fast') or int 
                width: 20,
                position: ['auto', 40] //x, y
                        //$('#element_to_pop_up_<?php //echo $t['Tubo']['id'];                                      ?>').bPopup()
            });
        });
    });
</script>